<?php
    
    session_start ();
    include 'function.php';
    display_header();
    if (isset($_SESSION['login']) && isset($_SESSION['mdp'])) {
    include 'include/Data.php';
    
        function creation($nom, $memoire, $ostype) {
            exec ("vboxmanage createvm --name '$nom' --ostype $ostype --register");          
            exec ("VBoxManage modifyvm '$nom' --memory '$memoire' --usb off --audio none ");
            exec ("VBoxManage modifyvm '$nom' --nic1 bridged --bridgeadapter1 eth0");
            exec ('mkdir /var/www/.VirtualBox/HardDisks/');
            
            if ($ostype == 'Debian') 
                {
                  echo exec ("vboxmanage clonehd /home/.VM/.ImagesHD/Debian6.0.vdi /var/www/.VirtualBox/HardDisks/'$nom'.vdi");
                }
                
            if ($ostype == 'Ubuntu') 
                {
                    echo exec ("vboxmanage clonehd /home/.VM/.ImagesHD/Ubuntu_11.04_32Bits.vdi /var/www/.VirtualBox/HardDisks/'$nom'.vdi");
                }
            exec ("VBoxManage storagectl '$nom' --name SCSI --add scsi ");
            exec ("VBoxManage storageattach '$nom' --storagectl SCSI --port 0 --device 0 --type hdd --medium '$nom'.vdi");
            
            
            $sql = "INSERT INTO machine_virtuel (Nom_machine,Os_Type,Taille_Ram,Started) VALUES ('".$nom."','".$ostype."','".$memoire."',0)";
            $req = mysql_query($sql);
            header('Location: affiche.php');
        }
        
    //htmlentities sur les variables récupérées en get
    $nom = htmlentities($_POST['nom']);
    $memoire = htmlentities($_POST['memoire']);
    $ostype = htmlentities($_POST['ostype']);
        
    echo "Nom: ".$nom." Memoire: ".$memoire." Ostype: ".$ostype;
    
    creation($nom,$memoire,$ostype);
    
    display_footer();
    } else { header("Refresh: 0; URL=index.php"); }
    is_log();
    
?>